﻿using System.Collections.Generic;
using UnityEngine;

public class EdgeChecker : UnitTerrainCheckerBase
{
    /// <summary>
    /// 是否接触墙面
    /// </summary>
    public bool isEdge { get; private set; }

    /// <summary>
    /// 碰撞箱
    /// </summary>
    public BoxCollider2D colliBox => m_ColliBox ?? (m_ColliBox = GetComponent<BoxCollider2D>());
    private BoxCollider2D m_ColliBox;

    private RaycastHit2D[] m_Hit2D;

    protected override void UpdateTerrainInfo(long _)
    {
        var direction = entity.animator.direction.x;
        var offset = Vector3.right * colliBox.size.x * 0.5f * direction;
        m_Hit2D = Physics2D.RaycastAll(transform.position + offset, Vector2.right * direction, 0.1f, layers);
        isEdge = m_Hit2D.Length > 0;
    }

    //private void OnDrawGizmosSelected()
    //{
    //    var direction = controller.fsm.direction.x;
    //    var offset = Vector3.right * colliBox.size.x * 0.5f * direction;
    //    var start = transform.position + offset;
    //    var end = start + Vector3.right * direction * 0.1f;
    //    Gizmos.DrawLine(start, end);
    //}
}